package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import controller.OrderController;

import model.Customer;
import model.CustomerList;
import model.InventoryList;
import model.OrderList;

/*************************************************************
 * Class: SimpleOrderGUI
 * @author JSmith1
 * 
 * description: An implementation of the simple order system
 * using a tabbed GUI. Supports adding customers, items and
 * orders as well as increasing stock of items.
 ************************************************************/
public class SimpleOrderGUI extends JTabbedPane{
	private static final long serialVersionUID = 4273267020336698694L;
	
	//global model information
	private static InventoryList inventory;
	private static OrderList orders;
	private static CustomerList customers;
	
	//the files to write to
	private static final String INVENTORY = "storage/inventoryList";
	private static final String ORDERS = "storage/orderList";
	private static final String CUSTOMERS = "storage/customerList";
	
	//customer components
	private JPanel customerDetails;
	private JButton addCustomer;
	private JTextArea custId;
	private JTextArea custName;
	private JTextArea address;
	
	//inventory components
	private JPanel inventoryDetails;
	private JButton addItem;
	private JButton addStock;
	private JTextArea itemId;
	private JTextArea itemName;
	private JTextArea quantity;
	private JTextArea lookupId;
	private JTextArea increaseBy;
	
	//order components
	private JPanel orderDetails;
	private JButton placeOrder;
	private JTextArea orderer;
	private JTextArea date;
	private JTextArea orderRows;
	
	/**
	 * constructor
	 */
	public SimpleOrderGUI(){
		super();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		////////////////////////////////////////////////////
		//customer content
		//instantiate the customerDetails panel that will
		//be nested inside the customer panel
		JPanel customerTab = new JPanel();
		customerTab.setLayout(new BoxLayout(customerTab, BoxLayout.Y_AXIS));
		customerDetails = new JPanel(new GridLayout(0,3));
		JLabel idLabel = new JLabel("Customer ID");
		JLabel nameLabel = new JLabel("Name"); 
		JLabel addressLabel = new JLabel("Address");
		
		//these labels represent the column headers
		customerDetails.add(idLabel);
		customerDetails.add(nameLabel);
		customerDetails.add(addressLabel);
		
		//create the form components for building a customer
		JLabel newIDLabel = new JLabel("Customer ID:");
		custId = new JTextArea();
		JLabel newNameLabel = new JLabel("Customer's Name:");
		custName = new JTextArea();
		JLabel newAddressLabel = new JLabel("Customer's Address");
		address = new JTextArea();
		
		//create the button and assign it a handler
		addCustomer = new JButton("Add Customer");
		addCustomer.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String currentID = custId.getText();
				String currentName = custName.getText();
				String currentAddress = address.getText();
				
				//instantiate a new customer and add it to the list
				Customer newCustomer = new Customer(currentID, currentName, currentAddress);
				
				customers.add(newCustomer);
				customerDetails.add(new JLabel(currentID));
				customerDetails.add(new JLabel(currentName));
				customerDetails.add(new JLabel(currentAddress));
			}
			
		});
		
		//add everything to the customer tab and add as a tab
		customerTab.add(customerDetails);
		customerTab.add(newIDLabel);
		customerTab.add(custId);
		customerTab.add(newNameLabel);
		customerTab.add(custName);
		customerTab.add(newAddressLabel);
		customerTab.add(address);
		customerTab.add(addCustomer);
		
		addTab("Customers", customerTab);
		//end customer content
		
		////////////////////////////////////////////////////////
		//inventory content
		//instantiate the inventoryDetails panel that will
		//be nested inside the inventory tab
		JPanel inventoryTab = new JPanel();
		inventoryTab.setLayout(new BoxLayout(inventoryTab, BoxLayout.Y_AXIS));
		inventoryDetails = new JPanel(new GridLayout(0,3));
		idLabel = new JLabel("Part #");
		nameLabel = new JLabel("Name"); 
		JLabel quantityLabel = new JLabel("Quantity");

		//these labels represent the column headers
		inventoryDetails.add(idLabel);
		inventoryDetails.add(nameLabel);
		inventoryDetails.add(quantityLabel);

		//create the form components for adding an item
		newIDLabel = new JLabel("Part #:");
		itemId = new JTextArea();
		newNameLabel = new JLabel("Item Name:");
		itemName = new JTextArea();
		JLabel newQuantityLabel = new JLabel("Quantity");
		quantity = new JTextArea();
		lookupId = new JTextArea(); //for adding stock to an existing id
		increaseBy = new JTextArea(); //the amount to increase stock by
		
		//create the buttons and assign handlers
		addItem = new JButton("Add New Item");
		addStock = new JButton("Add Stock");
		

		//add everything to the customer tab and add as a tab
		inventoryTab.add(inventoryDetails);
		inventoryTab.add(newIDLabel);
		inventoryTab.add(itemId);
		inventoryTab.add(newNameLabel);
		inventoryTab.add(itemName);
		inventoryTab.add(newQuantityLabel);
		inventoryTab.add(quantity);
		inventoryTab.add(addItem);
		inventoryTab.add(newIDLabel);
		inventoryTab.add(lookupId);
		inventoryTab.add(newQuantityLabel);
		inventoryTab.add(increaseBy);
		inventoryTab.add(addStock);

		addTab("Inventory", inventoryTab);
		//end inventoryContent

		///////////////////////////////////////////////////////////////
		//orderContent
		//instantiate the orderDetails panel that will
		//be nested inside the order panel 
		JPanel orderTab = new JPanel();
		orderTab.setLayout(new BoxLayout(orderTab, BoxLayout.Y_AXIS));
		orderDetails = new JPanel(new GridLayout(0,3));
		JLabel ordererLabel = new JLabel("Customer");
		JLabel dateLabel = new JLabel("Name"); 
		JLabel orderRowsLabel = new JLabel("Items");

		//these labels represent the column headers
		orderDetails.add(ordererLabel);
		orderDetails.add(dateLabel);
		orderDetails.add(orderRowsLabel);

		//create the form components for placing an order
		JLabel newOrdererLabel = new JLabel("Customer:");
		orderer = new JTextArea();
		JLabel newDateLabel = new JLabel("Date (mm/dd/yyyy):");
		date = new JTextArea();
		JLabel newItemsLabel = new JLabel("Items (item id, quantity):");
		orderRows = new JTextArea();

		//create the button and assign it a handler
		placeOrder = new JButton("Place Order");

		//add everything to the customer tab and add as a tab
		orderTab.add(orderDetails);
		orderTab.add(newOrdererLabel);
		orderTab.add(orderer);
		orderTab.add(newDateLabel);
		orderTab.add(date);
		orderTab.add(newItemsLabel);
		orderTab.add(orderRows);
		orderTab.add(placeOrder);

		addTab("Orders", orderTab);
		//end order content
	}

	
	/**
	 * getters and setters
	 */
	public static InventoryList getInventory() {
		return inventory;
	}

	public static void setInventory(InventoryList inventory) {
		SimpleOrderGUI.inventory = inventory;
	}

	public static OrderList getOrders() {
		return orders;
	}

	public static void setOrders(OrderList orders) {
		SimpleOrderGUI.orders = orders;
	}

	public static CustomerList getCustomers() {
		return customers;
	}

	public static void setCustomers(CustomerList customers) {
		SimpleOrderGUI.customers = customers;
	}
	
	/**
	 * main driver method
	 */
	public static void main(String args[]){
		OrderController controller = new OrderController();
		JFrame frame = new JFrame("Simple Order");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        initialize(controller);
        
        
		SimpleOrderGUI contentPane = new SimpleOrderGUI();
		
		frame.add(contentPane);
		frame.pack();
		frame.setVisible(true);
	}
	
	/**
	 * initialize
	 * @param an OrderController
	 * description: loads any saved inventory, customer, or order
	 * data.
	 */
	private static void initialize(OrderController controller){
		try{
			inventory = controller.initInventory(INVENTORY);
		}catch(FileNotFoundException fnf){
			System.out.println("Warning: The save file "+INVENTORY+" does not exist.");
			System.out.println("No pre-existing inventory data will be loaded.");
			inventory = new InventoryList();
		}try{
			orders = controller.initOrders(ORDERS);
		}catch(FileNotFoundException fnf){
			System.out.println("Warning: The save file "+ORDERS+" does not exist.");
			System.out.println("No pre-existing order data will be loaded.");
			orders = new OrderList();
		}try{
			customers = controller.initCustomers(CUSTOMERS);
		}catch(FileNotFoundException fnf){
			System.out.println("Warning: The save file "+CUSTOMERS+" does not exist.");
			System.out.println("No pre-existing order data will be loaded.");
			customers = new CustomerList();
		}
		
	}//end initialize
}
